package cn.tannn.ops.nginx.controller;

import cn.tannn.jdevelops.annotations.web.mapping.PathRestController;
import cn.tannn.jdevelops.exception.built.BusinessException;
import cn.tannn.jdevelops.jpa.constant.SQLOperator;
import cn.tannn.jdevelops.jpa.result.JpaPageResult;
import cn.tannn.jdevelops.result.response.ResultPageVO;
import cn.tannn.jdevelops.result.response.ResultVO;
import cn.tannn.jdevelops.result.utils.ListTo;
import cn.tannn.ops.common.constant.CloudEnums;
import cn.tannn.ops.nginx.controller.dto.CloudAccessKeyAdd;
import cn.tannn.ops.nginx.controller.dto.CloudAccessKeyPage;
import cn.tannn.ops.nginx.entity.CloudAccessKey;
import cn.tannn.ops.nginx.entity.Domains;
import cn.tannn.ops.nginx.service.CloudAccessKeyService;
import cn.tannn.ops.nginx.service.DomainsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 云厂商的AccessKey
 *
 * @author <a href="https://t.tannn.cn/">tan</a>
 * @version V1.0
 * @date 2024/8/29 下午1:45
 */
@PathRestController("/cloud/access")
@Tag(name = "云厂商Access管理")
@Slf4j
@RequiredArgsConstructor
public class CloudAccessKeyController {

    private final CloudAccessKeyService cloudAccessKeyService;
    private final DomainsService domainsService;

    @GetMapping("/clouds")
    @Operation(summary = "支持的clouds", description = "具体说明看代码注释")
    public ResultVO<List<Map<String, String>>> clouds() {
        List<Map<String, String>> values = CloudEnums.toMap();
        return ResultVO.success(values);
    }

    @GetMapping("/{id}")
    @Operation(summary = "根据ID获取详情", description = "info")
    public ResultVO<CloudAccessKey> info(@PathVariable("id") Long id) {
        CloudAccessKey bean = cloudAccessKeyService.findOnly("id", id).orElse(new CloudAccessKey());
        return ResultVO.success( bean);
    }

    @Operation(summary = "新增Access")
    @PostMapping("append")
    public ResultVO<String> append(@RequestBody @Valid CloudAccessKeyAdd append) {
        cloudAccessKeyService.findOnly("accessId",append.getAccessId()).ifPresent(v -> {
            throw new BusinessException("accessId:["+append.getAccessId()+"]重复");
        });
        cloudAccessKeyService.saveOneByVo(append);
        return ResultVO.success();
    }

    @Operation(summary = "分页查询Access")
    @PostMapping("page")
    public ResultPageVO<CloudAccessKey, JpaPageResult<CloudAccessKey>> page(@RequestBody @Valid CloudAccessKeyPage page) {
        Page<CloudAccessKey> byBean =  cloudAccessKeyService.findPage(page, page.getPage());
        JpaPageResult<CloudAccessKey> pageResult = JpaPageResult.toPage(byBean);
        return ResultPageVO.success(pageResult, "查询成功");
    }

    @Operation(summary = "Access集合")
    @GetMapping("lists")
    public ResultVO<List<CloudAccessKey>> lists() {
        List<CloudAccessKey> finds = cloudAccessKeyService.finds();
        return ResultVO.success(ListTo.to(CloudAccessKey.class,finds));
    }


    @Operation(summary = "删除Access")
    @GetMapping("delete")
    @Parameter(name = "accessId", description = "accessId", required = true)
    public ResultVO<String> delete(@RequestParam("accessId") String  accessId) {
        List<Domains> domains = domainsService.finds("accessId", SQLOperator.EQ, accessId);
        if(domains != null && !domains.isEmpty()){
            throw new BusinessException("当前Access被Domain使用中，请勿删除！");
        }
        cloudAccessKeyService.deleteEq("accessId",  accessId);
        return ResultVO.success();
    }

}
